const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headImg:'',
    userInfo: {},
    name:'',
    ifTeacher: true,
    stuIndex:0,
    homeData:[],
    studentId:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  setUserData(){
    var ifTeacher = this.data.ifTeacher
    if(ifTeacher){
      var headUrl = "/images/tx.png"
      if(this.data.userInfo.headUrl!=null){
        headUrl = this.data.userInfo.headUrl
      }
      this.setData({
        name:(this.data.userInfo.realName!=null?"您好，"+this.data.userInfo.realName+"老师":"您好，老师"),
        headImg:headUrl
      })
      console.log(this.data.name+" ===22= "+this.data.headImg);
    }else{
      var tHeadImge
      var vName =''
      if(this.data.userInfo.list!=null && this.data.userInfo.list.length>0 ){       
        if(this.data.userInfo.list[this.data.stuIndex].img !=null){
           tHeadImge = this.data.userInfo.list[this.data.stuIndex].img
        }else{
           if(this.data.userInfo.list[this.data.stuIndex].sex == 1){
             tHeadImge = "/images/tx.png"
           }else{
             tHeadImge = "/images/txn.png"
           }
        }
        vName = "您好，"+this.data.userInfo.list[this.data.stuIndex].name+this.data.userInfo.list[this.data.stuIndex].identity
        this.setData({
          studentId:this.data.userInfo.list[this.data.stuIndex].studentId,
        })
      }else{
        vName = "您好，家长"
        tHeadImge = "/images/tx.png"
      }

      this.setData({
        name:vName,
        headImg:tHeadImge
      })
      console.log(this.data.name+" ==== "+this.data.headImg);
    }
  },

  //头像加载失败
  imgerr(e){
    var ifTeacher = this.data.ifTeacher
    var tHeadImge
    if(ifTeacher){
      tHeadImge = "/images/tx.png"
    }else{
      if(this.data.userInfo.list!=null && this.data.userInfo.list.length>this.data.stuIndex ){       
           if(this.data.userInfo.list[this.data.stuIndex].sex == 1){
             tHeadImge = "/images/tx.png"
           }else{
             tHeadImge = "/images/txn.png"
           }    
      }else{    
        tHeadImge = "/images/tx.png"
      }
    }
    this.setData({
      headImg:tHeadImge
    })
  },

  //课堂记录
  toKetangjilu:function(){
    if(wx.getStorageSync('ifTeacher')){
      wx.navigateTo({
        url: '/pages/teacherKetangjilu/teacherKetangjilu',
      })
    }else{
      if(this.data.userInfo!=null && this.data.userInfo.list!=null && this.data.userInfo.list.length >0){
        wx.navigateTo({
          url: '/pages/parentKetangjilu/parentKetangjilu',
        })
      }else{
        wx.showToast({
          title: '请先绑定小孩',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
     
    }
  },
  //运动处方
  toChufang(){
    if(wx.getStorageSync('ifTeacher')){
      wx.navigateTo({
        url: '/pages/record/exercisePrescription',
        //url: '/pages/selectClassPage/selectClassPage'
      })
    }else{
      if(this.data.userInfo!=null && this.data.userInfo.list!=null && this.data.userInfo.list.length >0){
        wx.navigateTo({
          url: '/pages/record/exercisePrescriptionDetail',  //家长处方详情
        })
      }else{
        wx.showToast({
          title: '请先绑定小孩',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
     
    }
  },

  //健康报告
  toBaogao(){
    if(wx.getStorageSync('ifTeacher')){
      wx.navigateTo({
        url: '/pages/teacherPhysicalHealthReport/teacherPhysicalHealthReport',
      })
    }else{
      if(this.data.userInfo!=null && this.data.userInfo.list!=null && this.data.userInfo.list.length >0){
        wx.navigateTo({
          url: '/pages/record/physicalHealthReport',  //健康报告
        })
      }else{
        wx.showToast({
          title: '请先绑定小孩',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
     
    }
  },

  //跳绳任务
  tiaoshentask(){
    wx.navigateTo({
      url: '/packJumpTask/teacher/TaskList/TaskList',
    })
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  clickDate(){
    
    var dateSelect = this.selectComponent("#dateselect")
    dateSelect.onShowAlert();
  },
  goWork(){//作业

    if(wx.getStorageSync('ifTeacher')){
          wx.navigateTo({
            url: '../record/classRecList',
          })
    }else{
      if(this.data.userInfo!=null && this.data.userInfo.list!=null && this.data.userInfo.list.length >0){
            wx.navigateTo({
              url: '../record/parentsWorkList',
            })

      }else{
            wx.showToast({
              title: '请先绑定小孩',
              icon: 'none',
              duration: 2000,
              mask:true
            })
      }
         
    }

    
  },
  //记录
  goJilu:function(ret){
    if(wx.getStorageSync('ifTeacher')){
      wx.navigateTo({
        url: '/pages/KetangjiluBanjiInfo/KetangjiluBanjiInfo?calssName='+ret.currentTarget.dataset.calssName+"&courseId="+ret.currentTarget.dataset.courseid+"&classId="+ret.currentTarget.dataset.classid,
      })
    }else{
      if(this.data.userInfo!=null && this.data.userInfo.list!=null && this.data.userInfo.list.length >0){
       
        wx.navigateTo({
          url: '/pages/KetangjiluInfo/KetangjiluInfo?studentId='+this.data.studentId+"&courseId="+ret.currentTarget.dataset.courseid+"&classId="+ret.currentTarget.dataset.classid,
         
        })
      }else{
        wx.showToast({
          title: '请先绑定小孩',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
      
    }
  },
  //课堂风采
  toKetangfengcai(){
    if(this.data.userInfo!=null && this.data.userInfo.list!=null && this.data.userInfo.list.length >0){
      wx.navigateTo({
        url: '/pages/ketangfengcai/ketangfengcai',
      })
    }else{
      wx.showToast({
        title: '请先绑定小孩',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      ifTeacher:wx.getStorageSync('ifTeacher'), 
      userInfo:wx.getStorageSync('userData'),
      stuIndex:wx.getStorageSync('stuIndex')
    })
    this.setUserData()
    //console.log('当前用户角色1:'+wx.getStorageSync('ifTeacher'))

    if(this.data.ifTeacher){
      this.getTeacherLis()
    }else{
      this.getParentList()
    }
  },


  getTeacherLis(){
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    
    app.api.getTeacherLis().then(rest => {
      wx.hideLoading()
     
      if(rest&&rest.success){   
        console.log('getTeacherLis接口数据='+JSON.stringify(rest))

         if(rest.data.list!=null && rest.data.list.length > 0){
          this.setData({
            homeData:rest.data.list
          })
         }
      
       
       
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  
  },
  
  getParentList(){
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
   
    app.api.getParentList().then(rest => {
      wx.hideLoading()
      
      if(rest&&rest.success){   
        console.log('getParentList接口数据='+JSON.stringify(rest))
         if(rest.data.list!=null && rest.data.list.length > 0){
       
          this.setData({
            homeData:rest.data.list
          })
         }
         if(this.data.homeData!=null && this.data.homeData.length>0){
          console.log('getParentList接口数据2='+JSON.stringify(this.data.homeData))
         }
         
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  
  },

  goWorkInfo(ret){
    var homeworkid  = ret.currentTarget.dataset.homeworkid
    var status = ret.currentTarget.dataset.status
    var index  = ret.currentTarget.dataset.index

    if(wx.getStorageSync('ifTeacher')){

      //进行中、已结束 status:0 进行中 1已结束
      wx.navigateTo({
        url: '../../pages/record/detail?info='+JSON.stringify(this.data.homeData[index]),
      })

      // //已发布
      // if(this.data.selectIndex == 2){
      //   wx.navigateTo({
      //     url: '../../pages/record/editTeacherWorkDetai?info='+JSON.stringify(this.data.switchList[this.data.selectIndex].data.list[e.currentTarget.dataset.index]),
      //   })
      // }else{
      //   //进行中、已结束
      //   wx.navigateTo({
      //     url: '../../pages/record/detail?info='+JSON.stringify(this.data.switchList[this.data.selectIndex].data.list[e.currentTarget.dataset.index]),
      //   })
      // }
     
    }else{
      if(status==0){//编辑
        wx.navigateTo({
          url: '../../pages/record/parentsWorkDetail?isEdit=1&info='+JSON.stringify(this.data.homeData[index]),
          })
        }else{
          wx.navigateTo({
            url: '../../pages/record/parentsWorkDetail?isEdit=0&info='+JSON.stringify(this.data.homeData[index]),
          })
        }
        
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})